package com.meida.module.bus.provider.handler;

import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusMaterial;
import com.meida.module.bus.client.entity.BusMaterialPlan200;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusVersion;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.service.BusMaterialService;
import com.meida.module.bus.provider.service.BusProjectService;
import com.meida.module.bus.provider.service.BusVersionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component(value = "busMaterialPlan200SaveHandler")
public class BusMaterialPlan200SaveHandler implements SaveInterceptor<BusMaterialPlan200> {
    @Autowired
    private BusMaterialService busMaterialService;
    @Autowired
    private BusVersionService busVersionService;
    @Autowired
    private BusProjectService busProjectService;
    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap requestMap) {
        if (FlymeUtils.isEmpty(requestMap.get("levelNo"))) {
            return ResultBody.failed("层级号不能为空");
        }else{
           String levelNo= requestMap.get("levelNo");
            if(levelNo.length()<11){
                return ResultBody.failed("层级号长度不正确");
            }
        }
        if (!requestMap.get("levelNo").toString().substring(0, 5).equals(requestMap.get("packageNo").toString())) {
            return ResultBody.failed("层级号与工程包代码不一致");
        }
        return ResultBody.ok();
    }
    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusMaterialPlan200 busMaterialPlan200) {
        Long materialId = IdWorker.getId();
        busMaterialPlan200.setMaterialId(materialId);
        Long versionId = IdWorker.getId();
        criteriaSave.put("materialId", materialId);
        criteriaSave.put("versionId", versionId);
        System.out.println("materialId1:" + materialId);
        String materialCode=busMaterialPlan200.getMaterialCode();
        String[] strs=materialCode.split(" ");
        if(FlymeUtils.isNotEmpty(busMaterialPlan200.getInstructNumber())){
            busMaterialPlan200.setPlanType(2);//追加
        }else{
            busMaterialPlan200.setPlanType(1);//修正
        }
        busMaterialPlan200.setMaterialCode(strs[0]);
        busMaterialPlan200.setMaterialName(strs[1]);
        busMaterialPlan200.setVersionId(versionId);
        busMaterialPlan200.setCreateTime(DateUtils.getNowDateTime());
        busMaterialPlan200.setStatus("2");
        busMaterialPlan200.setOrderflg("0");
        busMaterialPlan200.setCostQuantity100(BigDecimal.ZERO);
        busMaterialPlan200.setVersionStatus(ApiConstants.INT_ZERO);//先设置为临时版本，审核通过之后在置为正式版本
        busMaterialPlan200.setApplayStatus(ApiConstants.WZ_APPLYSTATUS_AUDIT_WAIT);//待审核
        busMaterialPlan200.setZtPurchaseCount(BigDecimal.ZERO);
        busMaterialPlan200.setDhPurchaseCount(BigDecimal.ZERO);
        busMaterialPlan200.setPurchaseCount(BigDecimal.ZERO);
        busMaterialPlan200.setParentId(0L);
        busMaterialPlan200.setPlanPurchaseCount(busMaterialPlan200.getCostQuantity200().multiply(busMaterialPlan200.getLoss()));
//        busMaterialPlan200.setCostdisplayQuantity100ty100100ity200(busMaterialPlan200lan200.getCostQuantity200());
        busMaterialPlan200.setEndPurchaseCount(busMaterialPlan200.getCostQuantity200().multiply(busMaterialPlan200.getLoss()));
        busMaterialPlan200.setDisplayplanPurchaseCount(busMaterialPlan200.getCostQuantity100().multiply(busMaterialPlan200.getLoss()));
    }
    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusMaterialPlan200 entity) {
        EntityMap requestMap = cs.getRequestMap();
        Long projectId = requestMap.getLong("projectId");
        Long versionId = cs.getLong("versionId");
        BusProject project=busProjectService.getById(projectId);
        BusVersion latestVersion = busVersionService.getLatestVersion(projectId,1);
        BusVersion newBusVersion = new BusVersion();
        if (FlymeUtils.isNotEmpty(latestVersion)) {
            BeanUtils.copyProperties(latestVersion, newBusVersion);
            String smallVerion = newBusVersion.getSmallVerion();
            Integer small = TypeUtils.castToInt(smallVerion) + 1;
            if (small < 10) {
                smallVerion = "0" + small;
            } else {
                smallVerion = TypeUtils.castToString(small);
            }
            newBusVersion.setSmallVerion(smallVerion);
            newBusVersion.setUserId(OpenHelper.getUserId());
            newBusVersion.setVersionId(versionId);
            newBusVersion.setCreateTime(DateUtils.getNowDateTime());
            newBusVersion.setState(0);//审核版本
            newBusVersion.setVersionType(1);//材料版本
            newBusVersion.setProjectId(projectId);
        }
        project.setVersionId(versionId);
        project.setUserId(OpenHelper.getUserId());
        busVersionService.save(newBusVersion);
        Long materialId = requestMap.getLong("materialId");
        BusMaterial busMaterial = cs.getEntity(BusMaterial.class);
        busMaterial.setParentId(0L);
        busMaterial.setMaterialId(materialId);
        busMaterial.setPlanPurchaseCount(entity.getCostQuantity200().multiply(entity.getLoss()));
        busMaterial.setCreateTime(DateUtils.getNowDateTime());
        busMaterialService.save(busMaterial);
        busProjectService.saveOrUpdate(project);
    }
}
